﻿using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight;
using ProjectWizardManagement.Entity.Model;

namespace ProjectWizardManagementv1.ViewModels.Concrete
{
    public class ProjectFileViewModel : ViewModelBase
    {
        #region Members
        private readonly ProjectFile _projectFile;
        private ProjectFileViewModel _parentFile;
        private readonly ObservableCollection<ProjectFileViewModel> _childrenFiles;
        private bool _isSelected;
        private bool _isMultiSelected;
        #endregion

        #region Properties Static

        public static string ProjectDirectoryPath { get; set; }
        #endregion

        #region Properties
        public string Name
        {
            get { return _projectFile.Name; }
            set { _projectFile.Name = value; RaisePropertyChanged("Name"); }
        }

        public string Extension
        {
            get { return _projectFile.Extension; }
            set { _projectFile.Extension = value; RaisePropertyChanged("Extension"); }
        }

        public string Path
        {
            get { return _projectFile.Path; }
            set
            {
                if (IsDirectory)
                    PathReconstruction(_projectFile.Path, value);
                _projectFile.Path = value; 
                RaisePropertyChanged("Path");
            }
        }

        public bool IsDirectory
        {
            get { return _projectFile.IsDirectory; }
            set { _projectFile.IsDirectory = value; }
        }

        public string FileName
        {
            get { return _projectFile.FileName; }
        }

        public ObservableCollection<ProjectFileViewModel> Children
        {
            get { return _childrenFiles; }
        }

        public ImageSource FileIcon
        {
            get
            {
                if (!IsDirectory && File.Exists(Path))
                {
                    var icon = Icon.ExtractAssociatedIcon(Path);
                    if (icon != null)
                        return Imaging.CreateBitmapSourceFromHIcon(
                            icon.Handle, new Int32Rect(0,0,icon.Width, icon.Height ), BitmapSizeOptions.FromEmptyOptions());
                }


                return null;
            }
        }

        public bool IsSelected
        {
            get { return _isSelected || IsMultiSelected; }
            set { _isSelected = value; RaisePropertyChanged("IsSelected"); }
        }

        public bool IsOpen { get; set; }

        public bool IsMultiSelected
        {
            get { return _isMultiSelected; }
            set { _isMultiSelected = value; RaisePropertyChanged("IsMultiSelected"); RaisePropertyChanged("IsSelected"); }
        }

        #endregion

        public ProjectFileViewModel(ProjectFile file) : this(file, null)
        {

        }

        private ProjectFileViewModel(ProjectFile file, ProjectFileViewModel parent)
        {
            _parentFile = parent;
            _projectFile = file;

            _childrenFiles = new ObservableCollection<ProjectFileViewModel>
                                 (
                                     file.ProjectFiles.Select(child => new ProjectFileViewModel(child, this)).ToList()
                                 );
        }

        public void AddToChildren(ProjectFile file)
        {
            _projectFile.ProjectFiles.Add(file);
            var fileViewModel = new ProjectFileViewModel(file, this);
            Children.Add(fileViewModel);
            RaisePropertyChanged("Children");
        }

        public void RemoveFromChildren(ProjectFileViewModel file)
        {
            var fileToRemove = _projectFile.ProjectFiles.Select(f => f).First(f => f.FileName == file.FileName);
            _projectFile.ProjectFiles.Remove(fileToRemove);
            var fileModelToRemove = Children.Select(p => p).First(p => p.FileName == file.FileName);
            Children.Remove(fileModelToRemove);
            RaisePropertyChanged("Children");
        }

        private void PathReconstruction(string oldPath, string newPath)
        {
            foreach (var file in Children)
                file.Path = file.Path.Replace(oldPath, newPath);
        }

        public override string ToString()
        {
            return FileName;
        }
    }
}
